package com.sgb.kjwl.pop;

import android.content.Context;
import android.text.Editable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.MyTextWatcher;
import com.swgk.core.util.UiUtil;

import razerdp.basepopup.BasePopupWindow;

/**
 * 作者:张磊
 * 日期:2019/10/24
 * 说明:输入数量弹框
 */
public class InputPop extends BasePopupWindow {

    public View mTvSure;
    public EditText mEtInputNumber;
    public double mLowestBuy;

    public InputPop(Context context) {
        super(context);
    }

    @Override
    public View onCreateContentView() {
        View v = createPopupById(R.layout.dialog_input_pop);
        mEtInputNumber = v.findViewById(R.id.et_input_number);
        mTvSure = v.findViewById(R.id.tv_sure);
        v.findViewById(R.id.tv_cancel).setOnClickListener(v1 -> {
            InputMethodManager imm = (InputMethodManager) UiUtil.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mEtInputNumber.getWindowToken(), 0);
            dismiss();
        });
        mEtInputNumber.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                UiUtil.input(s, mEtInputNumber, mLowestBuy, "不能低于最低采购量");
            }

            @Override
            public void afterTextChanged(Editable s) {
                String temp = s.toString();
                int posDot = temp.indexOf(".");
                if (posDot < 0) {
                    return;
                }
                //输入以 “ . ”开头的情况，自动在.前面补0
                if (temp.startsWith(".") && posDot == 0) {
                    s.insert(0, "0");
                    return;
                }
                //输入"08" 等类似情况
                if (temp.startsWith("0") && temp.length() > 1 && (posDot == -1 || posDot > 1)) {
                    s.delete(0, 1);
                    return;
                }
                //小数点后面只能有两位小数
                if (temp.length() - posDot - 1 > 3) {
                    s.delete(posDot + 4, posDot + 5);
                }
            }
        });

        return v;
    }
}
